/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Operations;

import IFSv2.Connection.DBConnection;
import IFSv2.Forms.Main.frmMain;
import IFSv2.Forms.Utilities.frmUploadExcel;
import IFSv2.Models.mdlCurrency;
import IFSv2.Models.mdlLogin;
import IFSv2.Models.mdlMain;
import IFSv2.Util.utilErrorHandler;
import java.awt.Component;
import java.lang.reflect.InvocationTargetException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JToolBar;

/**
 *
 * @author Joel
 */
public class clsMain {
    
    private final String FORM_PATH = "IFSv2.Forms.";
    
    frmMain mainForm;
    JToolBar toolBar;
    
    public void setToolBar(JToolBar toolBar){
        this.toolBar = toolBar;
    }
    
    public void setMainForm(frmMain mainForm){
        this.mainForm = mainForm;
    }
    
    public JComponent loadPanel(String componentTitle){
        JComponent selectedComponent = null;
        try {            
            selectedComponent = (JComponent)Class.forName(FORM_PATH+componentTitle).getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            utilErrorHandler.printError("No Class Definition Found", "clsMenu", "openPanel");
        } catch (ClassNotFoundException | NoSuchMethodException | SecurityException ex) {
            utilErrorHandler.printError("No Class Definition Found", "clsMenu", "openPanel");
        }
        if(selectedComponent == null){
            utilErrorHandler.printError("No Class Definition Found", "clsMenu", "openPanel");
        }
        
        
        return selectedComponent;
    }
    
    public static Component getPanel(String componentTitle){
        return mdlMain.tabMap.getForward(componentTitle);
    }
    
    public static void openTab(String title, JComponent thisComponent){  
        if(mdlMain.tabMap.containsKey(title)){
            frmMain.menuTabbedPane.setSelectedIndex(findTab(title));
        }else{
            frmMain.menuTabbedPane.add(thisComponent);
            frmMain.menuTabbedPane.setSelectedComponent(thisComponent);
            frmMain.menuTabbedPane.setTabComponentAt(frmMain.menuTabbedPane.indexOfComponent(thisComponent), new clsTab(title, thisComponent));
            mdlMain.tabMap.put(title, thisComponent);
        }
    }

    public static int findTab(String title){
        
        for(int i=0; i<frmMain.menuTabbedPane.getTabCount(); i++){
            if(title.equals(((clsTab)frmMain.menuTabbedPane.getTabComponentAt(i)).getTitle())){
                return i;
            }
        }
        return 0;
    }
    
    public Map<String, String> programMap = new HashMap();

    public String[] getMenuList(String module){
    
        ArrayList<String> modList = new ArrayList<>(); 
        
        String _sql = "SELECT ProgramMenu, ProgramMenuForm FROM sytreemenu WHERE ProgramModule = ? ORDER BY LineSeqID";
        try {
            PreparedStatement preparedStatement = DBConnection.getActiveConnection().prepareStatement(_sql);
            preparedStatement.setString(1, module);
            
            try (ResultSet rsList = preparedStatement.executeQuery()) {
                while(rsList.next()){
                    
                    programMap.put(rsList.getString("ProgramMenu"), rsList.getString("ProgramMenuForm"));
                    modList.add(rsList.getString("ProgramMenu"));
                    
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(clsMain.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        String [] _return = new String[modList.size()];
        _return = modList.toArray(_return);

        return _return;
    }
    
//    public String[] getMenuList(){
//    
//        HashMap<String, javax.swing.JPanel>  
//        programMap.
//    }
    
    public static void openUtilUploadExcel(){
        frmUploadExcel uploadExcel = new frmUploadExcel();
        openTab("Upload Excel", uploadExcel);
    }
    
    public void getCurrency(){
        String _sql = "SELECT CurrencyCode, CurrencySymbol, ExchangeRate FROM currencycode WHERE HomeCurrency = 1";
        try {
            try (java.sql.ResultSet rsCurrency = DBConnection.getActiveConnection().createStatement().executeQuery(_sql)) {
                rsCurrency.next();
                mdlCurrency.currencyCode = rsCurrency.getString("CurrencyCode");
                mdlCurrency.currencySymbol = rsCurrency.getString("CurrencySymbol");
                mdlCurrency.exchangeRate = rsCurrency.getDouble("ExchangeRate");
            }
        } catch (SQLException ex) {
            utilErrorHandler.printError("Unable to retrieve default currency", "clsMain", "getCurrency");
            Logger.getLogger(frmMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateInfoBar(){
        getCurrency();
        frmMain.lblCurrDB.setText(mdlLogin.dbServer);
        frmMain.lblCurrDBUsed.setText(mdlLogin.database);
        frmMain.lblCurrUsername.setText(mdlLogin.username);
        frmMain.lblCurrCurrency.setText(mdlCurrency.currencyCode);
        
    }
}